package com.atguigu.gulimail.member.service.nacosprovider;

import com.atguigu.common.exception.BizCodeEnum;
import com.atguigu.common.service.member.ProviderMemberServie;
import com.atguigu.common.utils.R;
import com.atguigu.common.vo.UserLogin;
import com.atguigu.gulimail.member.dao.MemberDao;
import com.atguigu.gulimail.member.dao.MemberReceiveAddressDao;
import com.atguigu.gulimail.member.entity.MemberEntity;
import com.atguigu.gulimail.member.entity.MemberReceiveAddressEntity;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/**
 * @author fangy
 * @date 2022-10-10 22:45
 */
@DubboService
public class MemberServiceimpl implements ProviderMemberServie {

    @Autowired
    MemberDao memberDao;

    @Autowired
    MemberReceiveAddressDao memberReceiveAddressDao;


    @Override
    public R loginByUserLogin(UserLogin userLogin) {

        System.out.println(userLogin);

        String username = userLogin.getUsername();
        String password = userLogin.getPassword();

        MemberEntity memberEntity = memberDao.selectOne(new QueryWrapper<MemberEntity>().eq("username", username));

        if(memberEntity != null){
            BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
            boolean b = encoder.matches(password,memberEntity.getPassword());
            if (b){
                return R.ok().setData(memberEntity);
            }else {
                return R.error(BizCodeEnum.ERROR_PASSWORD_EXCEPTION.getCode(),BizCodeEnum.ERROR_PASSWORD_EXCEPTION.getMsg());
            }
        }else {
            return R.error(BizCodeEnum.ERROR_USERNAME_EXCEPTION.getCode(),BizCodeEnum.ERROR_USERNAME_EXCEPTION.getMsg());
        }

    }

    @Override
    public R addrInfo(Long addrId) {

        MemberReceiveAddressEntity member_id = memberReceiveAddressDao.selectOne(new QueryWrapper<MemberReceiveAddressEntity>().eq("member_id", addrId));

        return R.ok().setData(member_id);
    }


}


